View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: StringConvertors.java,v 1.6 2002/09/05 15:41:47 ludovicc Exp $ 37 */ 38 package org.scopemvc.util.convertor; 39 40 41 import java.util.HashMap; 42 import java.util.Iterator; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.scopemvc.util.ScopeConfig; 46 47 /*** 48 * Factory class for creation of default convertors for arbitrary classes. <br> 49 * You can use a custom factory with a different strategy for locating the 50 * convertors. For that, put the class name of your factory in the 51 * StringConvertors property in the Scope configuration. 52 * 53 * @author <A HREF="mailto:danmi@users.sourceforge.net">Daniel Michalik</A> 54 * @created 15 July 2002 55 * @version $Revision: 1.6 $ $Date: 2002/09/05 15:41:47 $ 56 */ 57 public class StringConvertors { 58 59 private final static Log LOG = LogFactory.getLog(StringConvertors.class); 60 private final static String CONVERTORS_PROPERTY = "StringConvertors"; 61 private final static String CONVERTOR_PROPERTY_PREFIX = "StringConvertor."; 62 private static StringConvertors instance; 63 64 private HashMap defaultConvertors; 65 66 /*** 67 * Load convertors from ScopeConfig. 68 */ 69 static { 70 try { 71 Class scClass = Class.forName(ScopeConfig.getString(CONVERTORS_PROPERTY)); 72 instance = (StringConvertors) scClass.newInstance(); 73 } catch (Exception ex) { 74 LOG.error("Could not create the StringConvertors factory of class " + ScopeConfig.getString(CONVERTORS_PROPERTY), ex); 75 instance = new StringConvertors(); 76 } 77 } 78 79 /*** 80 * Load convertors from ScopeConfig. 81 */ 82 public StringConvertors() { 83 // this constructor needs to be public for reflection 84 defaultConvertors = new HashMap(); 85 for (Iterator iter = ScopeConfig.getKeysMatching(CONVERTOR_PROPERTY_PREFIX); 86 iter.hasNext(); ) { 87 String key = (String) iter.next(); 88 Class convertorClass = ScopeConfig.getClass(key); 89 if (convertorClass == null) { 90 LOG.error("Null StringConvertor class in config for: " + key); 91 continue; 92 } 93 try { 94 defaultConvertors.put(key.substring( 95 CONVERTOR_PROPERTY_PREFIX.length()), 96 convertorClass.newInstance()); 97 } catch (Exception e) { 98 LOG.error("Failed to create StringConvertor for: " + key, e); 99 } 100 } 101 if (LOG.isDebugEnabled()) { 102 LOG.debug("StringConvertor.<clinit>: " + defaultConvertors.size()); 103 } 104 } 105 106 /*** 107 * Return a {@link StringConvertor} for the passed Class, else return null. 108 * <br> 109 * Note that this doesn't work for subclasses of datatypes types: the type 110 * must match exactly. 111 * 112 * @param inValueClass Description of the Parameter 113 * @return Description of the Return Value 114 */ 115 public static StringConvertor forClass(Class inValueClass) { 116 return instance.findConvertor(inValueClass); 117 } 118 119 /*** 120 * Return a {@link StringConvertor} for the passed Class, else return null. 121 * <br> 122 * The default implementation doesn't work for subclasses of datatypes 123 * types: the type must match exactly. 124 * 125 * @param inValueClass Description of the Parameter 126 * @return Description of the Return Value 127 */ 128 protected StringConvertor findConvertor(Class inValueClass) { 129 // if (Debug.ON) Debug.assert(convertors != null && (convertors.get(inValueClass.getName()) == null || convertors.get(inValueClass.getName()) instanceof StringConvertor)); 130 return (StringConvertor) defaultConvertors.get(inValueClass.getName()); 131 } 132 133 /*** 134 * Register a new convertor for the value class. 135 * 136 * @param inValueClass The value class with the convertor to register 137 * @param inConvertor The StringConvertor instance for the value class. 138 */ 139 protected void registerConvertor(Class inValueClass, StringConvertor inConvertor) { 140 defaultConvertors.put(inValueClass.toString(), inConvertor); 141 } 142 }

This page was automatically generated by Maven